package com.usian.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.mapper.TbContentCategoryMapper;
import com.usian.mapper.TbContentMapper;
import com.usian.pojo.TbContent;
import com.usian.pojo.TbContentCategory;
import com.usian.pojo.TbContentCategoryExample;
import com.usian.pojo.TbContentExample;
import com.usian.redis.RedisClient;
import com.usian.service.ContentService;
import com.usian.utils.AdNode;
import com.usian.utils.PageResult;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@Transactional
public class ContentServcieImpl implements ContentService{
    @Value("${AD_CATEGORY_ID}")
    private Long AD_CATEGORY_ID;

    @Value("${AD_HEIGHT}")
    private Integer AD_HEIGHT;

    @Value("${AD_WIDTH}")
    private Integer AD_WIDTH;

    @Value("${AD_HEIGHTB}")
    private Integer AD_HEIGHTB;

    @Value("${AD_WIDTHB}")
    private Integer AD_WIDTHB;
    @Autowired
    private RedisClient redisClient;
    @Value("${PROTAL_CATRESULT_KEYS}")
    private String PROTAL_CATRESULT_KEYS;
    @Value("${PROTAL_CATRESULT_KEY}")
    private String PROTAL_CATRESULT_KEY;


    @Autowired
    private TbContentCategoryMapper tbContentCategoryMapper;
    @Autowired
    private TbContentMapper tbContentMapper;


    /**
     * 查询内容管理分类
     * @param id
     * @return
     */
    @Override
    public List<TbContentCategory> selectContentCategoryByParentId(Integer id) {
        TbContentCategoryExample example= new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(Long.valueOf(id));
        List<TbContentCategory> list = tbContentCategoryMapper.selectByExample(example);
        return list;
    }

    /**
     * 添加管理接口
     * @param parentId
     * @param name
     * @return
     */
    @Override
    public Integer insertContentCategory(Integer parentId, String name) {
        redisClient.del(PROTAL_CATRESULT_KEY);
        TbContentCategory tb=new TbContentCategory();
        tb.setUpdated(new Date());
        tb.setCreated(new Date());
        tb.setStatus(1);
        tb.setIsParent(false);
        tb.setParentId(Long.valueOf(parentId));
        tb.setName(name);
        tb.setSortOrder(1);
        int i = tbContentCategoryMapper.insertSelective(tb);

        TbContentCategory contentCategory = this.tbContentCategoryMapper.selectByPrimaryKey(tb.getParentId());
        if (!contentCategory.getIsParent()){
            contentCategory.setIsParent(true);
            contentCategory.setUpdated(new Date());
            tbContentCategoryMapper.updateByPrimaryKeySelective(contentCategory);
        }

        return i;
    }

    /**
     * 修改内容管理分类
     * @param id
     * @param name
     * @return
     */
    @Override
    public Integer updateContentCategory(Integer id, String name) {
        redisClient.del(PROTAL_CATRESULT_KEY);
        TbContentCategory tbContentCategory = tbContentCategoryMapper.selectByPrimaryKey(Long.valueOf(id));
        tbContentCategory.setName(name);

        int i = tbContentCategoryMapper.updateByPrimaryKey(tbContentCategory);
        return i;
    }

    /**
     * 删除内容管理分类
     * @param categoryId
     * @return
     */
    @Override
    public Integer deleteContentCategoryById(Integer categoryId) {

        redisClient.del(PROTAL_CATRESULT_KEY);

        //查询出要删的那条信息
        TbContentCategory tbContentCategory = tbContentCategoryMapper.selectByPrimaryKey(Long.valueOf(categoryId));
        //根据父id查询出有几条信息
        TbContentCategoryExample example= new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(tbContentCategory.getParentId());
        List<TbContentCategory> list = tbContentCategoryMapper.selectByExample(example);
        //判断list的长度
        if (list.size()>1){
            int i = tbContentCategoryMapper.deleteByPrimaryKey(Long.valueOf(categoryId));
            return i;
        }else{
            //查询出父的id
            TbContentCategory tbContentCategory1 = tbContentCategoryMapper.selectByPrimaryKey(tbContentCategory.getParentId());
            tbContentCategory1.setIsParent(false);
            tbContentCategory1.setUpdated(new Date());
            tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory1);
            int i = tbContentCategoryMapper.deleteByPrimaryKey(Long.valueOf(categoryId));
            return i;
        }
    }

    @Override
    public PageResult selectTbContentAllByCategoryId(Integer page, Integer rows, Long categoryId) {
        PageHelper.startPage(page, rows);
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        List<TbContent> list = this.tbContentMapper.selectByExample(example);
        PageInfo<TbContent> pageInfo = new PageInfo<TbContent>(list);
        PageResult result = new PageResult();
        result.setPageIndex(pageInfo.getPageNum());
        result.setTotalPage((int) pageInfo.getTotal());
        result.setResult(pageInfo.getList());
        return result;

    }

    @Override
    public Integer insertTbContent(TbContent tbContent) {
        tbContent.setUpdated(new Date());
        tbContent.setCreated(new Date());
        redisClient.hdel(PROTAL_CATRESULT_KEYS,AD_CATEGORY_ID.toString());
        return this.tbContentMapper.insertSelective(tbContent);
    }

    @Override
    public List<AdNode> selectFrontendContentByAD() {
        //先从缓存中读取
        List<AdNode> tbContentList1=(List<AdNode>)redisClient.hget(PROTAL_CATRESULT_KEYS,AD_CATEGORY_ID.toString());
        //判断缓存中是否有数据
        if (tbContentList1!=null){
            return tbContentList1;
        }

        // 查询TbContent
        TbContentExample tbContentExample = new TbContentExample();
        TbContentExample.Criteria criteria = tbContentExample.createCriteria();
        criteria.andCategoryIdEqualTo(AD_CATEGORY_ID);
        List<TbContent> tbContentList = tbContentMapper.selectByExample(tbContentExample);

        List<AdNode> adNodeList = new ArrayList<AdNode>();
        for (TbContent tbContent : tbContentList) {
            AdNode adNode = new AdNode();
            adNode.setSrc(tbContent.getPic());
            adNode.setSrcB(tbContent.getPic2());
            adNode.setHref(tbContent.getUrl());
            adNode.setHeight(AD_HEIGHT);
            adNode.setWidth(AD_WIDTH);
            adNode.setHeightB(AD_HEIGHTB);
            adNode.setWidthB(AD_WIDTHB);
            adNodeList.add(adNode);
        }

        redisClient.hset(PROTAL_CATRESULT_KEYS,AD_CATEGORY_ID.toString(),adNodeList);
        return adNodeList;
    }


}
